/* jQuery no conflict */
var $j = jQuery.noConflict();

// Time for user inactivity
idleTime = 0;

$j(document).ready(function() {
	
	// Increment the idle time counter every minute.
    var idleInterval = setInterval("timerIncrement()", 60000);
    
    // Zero the idle timer on mouse movement.
	$j(this).mousemove(function (e) {
        idleTime = 0;
    });
	
    $j(this).keypress(function (e) {
        idleTime = 0;
    });
	
	$j("#neverdue").click(function(e) {
		var endField = $j("#end");
		endField.val('');
		endField.toggleDisabled();
	});
	
	$j('#companies-hierarchy-ul .delete').click(function(e) {
		validate(e);
	});
	
	$j(".tools .delete").click(function(e) {
		validate(e);
	});
	
	$j('.report-delete').click(function (e) {
		var check = confirm('¿Estas seguro de querer borrar este reporte?');
		if ( check ) {
			return true;
		} else {
			e.preventDefault();
		}
	});
	
	$j.datepicker.setDefaults( $j.datepicker.regional[ "es" ] );
	
	$j( "#date" ).datepicker({
      defaultDate: "today",
      dateFormat: "yy-mm-dd",
      changeMonth: true,
      numberOfMonths: 2,
      onClose: function( selectedDate ) {
        $j( "#date_end" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    
    $j("#date_end").datepicker({
      defaultDate: "today",
      dateFormat: "yy-mm-dd",
      changeMonth: true,
      numberOfMonths: 2,
      onClose: function( selectedDate ) {
        $j( "#date" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
	
	$j( "#date_range" ).click(function(e) {
		$j('#date_end').toggleDisabled();
		$j('#date-range-end-row').slideToggle();
	});
	
	var formSaveReport = $j('#form-save-report');
	formSaveReport.submit(function(e) {
		
		var data = $j(this).serialize();
		
		$j.post('managereports.php?' + data, function(r) {
			
			var dialog = $j('#dialog-report-message');
			
			$j('.dialog-msg', dialog).html(r.msg);
			
			$j( dialog ).dialog({
				modal: true, 
				draggable: false, 
				buttons: { 
					Aceptar: function() { 
						$j( this ).dialog( "close" ); 
					}
				}
			});
			
		}, 'json');
		
		e.preventDefault();
		
	});
	
});

// Session expire timer
function timerIncrement() {
	
	idleTime = idleTime + 1;
	var idleTimeLimit = $j('#idle-time-limit').html();
	
	if ( idleTimeLimit === undefined ) idleTimeLimit = 10;
	
	if ( idleTime > idleTimeLimit - 1 ) {
		
		$j('#session-expired').dialog({
			modal:true,
			buttons: {
				Aceptar: function() {
					$j(this).dialog("close");
				}
			},
			close: function(e, ui) {
				window.location.href = 'manageuser.php?action=logout';
			}
		});
		
    }
}

// Toggle disable/enable function
(function($j) {
    $j.fn.toggleDisabled = function(){
        return this.each(function(){
            this.disabled = !this.disabled;
        });
    };
})(jQuery);

function validate(e) {
	var check = confirm('¿Estas seguro de querer borrar este elemento?, se eliminarán tambien las areas relacionadas.');
	
	if ( check ) {
		return true;			
	} else {
		e.preventDefault();
	}
}
